package com.java.day09.homework.teamPK;

import com.java.day08.homework.pk.Fightable;
import com.java.day08.homework.pk.Hero;

import java.util.Random;

public abstract class Team {
    //队伍是否阵亡
    public static boolean TeamIsAlive(Fightable[] team){
        //存活队员数量
        int live = team.length;
        for (int i = 0; i < team.length; i++) {

            if(live==0){
                System.out.println("******************************************************");
                return false;
            }
            if (team[i]!=null&&team[i].isAlive()==false) {
                team[i]=null;
                System.out.println();
                System.out.println("英雄已经阵亡");
                System.out.println();
                live-=1;
            }

        }
        return true;
    }

    //队伍攻击
    public static boolean TeamFight(Fightable[] team1,Fightable[] team2){
        //随机队员攻击
        Random random = new Random();
        int ran = random.nextInt(team1.length);
        Hero hero = (Hero) team1[ran];
        if(hero!=null){
            System.out.println(hero.getName()+"发起了攻击");
            TeamTakeDamage(hero.getAttack(),team2);
            return true;
        }else{
            TeamFight(team1,team2);
            return true;
        }

    }

    //受到伤害
    public static boolean TeamTakeDamage(int damage,Fightable[] team){
        //随机队员受到伤害
        Random random = new Random();
        int ran = random.nextInt(team.length);
        Hero hero = (Hero) team[ran];
        if(hero!=null){
            System.out.println(hero.getName()+"受到"+damage+"点伤害");
            hero.setLife(hero.getLife()-damage);
            return true;
        }else{
            TeamTakeDamage(damage,team);
            return true;
        }
    }
}
